﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Creating classes</title>
<meta name="GENERATOR" content="WinCHM">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>

<body>
<UL 
style="WORD-SPACING: 0px; FONT: medium 'Times New Roman'; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">
  <LI>
  <H2>@interface</H2></LI>
  <UL>
    <LI><SPAN class=notice>Based on an example in "Programming in Objective-C," 
    Copyright © 2004 by Sams Publishing. Used with permission</SPAN></LI>
    <LI>Fraction.h<PRE class=code style="BORDER-RIGHT: gray 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: gray 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: gray 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: gray 1px solid; BACKGROUND-COLOR: rgb(238,238,238)">#import &lt;Foundation/NSObject.h&gt;

@interface Fraction: NSObject {
    int numerator;
    int denominator;
}

-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(int) numerator;
-(int) denominator;
@end
</PRE></LI>
    <LI>NSObject: Short for NeXTStep Object. Although this is less meaningful 
    today since it's really OpenStep.</LI>
    <LI>Inheritance is specified as Class: Parent, as seen with Fraction: 
    NSObject.</LI>
    <LI>Instance variables go between @interface Class: Parent { .... }</LI>
    <LI>No access is set (protected, public, private). Default is protected. 
    Setting the access will be shown later</LI>
    <LI>Instance methods follow after the member variables. The format is: scope 
    (returnType) methodName: (parameter1Type) parameter1Name;</LI>
    <UL>
      <LI>scope refers to class or instance. instance methods begin with<SPAN 
      class=Apple-converted-space>&nbsp;</SPAN><B>-</B><SPAN 
      class=Apple-converted-space>&nbsp;</SPAN>class level methods begin 
      with<SPAN class=Apple-converted-space>&nbsp;</SPAN><B>+</B></LI></UL>
    <LI>Interface ends with @end</LI></UL>
  <LI>
  <H2>@implementation</H2></LI>
  <UL>
    <LI><SPAN class=notice>Based on an example in "Programming in Objective-C," 
    Copyright © 2004 by Sams Publishing. Used with permission</SPAN></LI>
    <LI>Fraction.m<PRE class=code style="BORDER-RIGHT: gray 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: gray 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: gray 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: gray 1px solid; BACKGROUND-COLOR: rgb(238,238,238)">#import "Fraction.h"
#import &lt;stdio.h&gt;

@implementation Fraction
-(void) print {
    printf( "%i/%i", numerator, denominator );
}

-(void) setNumerator: (int) n {
    numerator = n;
}

-(void) setDenominator: (int) d {
    denominator = d;
}

-(int) denominator {
    return denominator;
}

-(int) numerator {
    return numerator;
}
@end</PRE></LI>
    <LI>@implementation ClassName starts the implementation @end ends it</LI>
    <LI>All the defined methods are implemented very simlar to how they are 
    declared in the interface</LI></UL>
  <LI>
  <H2>Piecing it together</H2></LI>
  <UL>
    <LI><SPAN class=notice>Based on an example in "Programming in Objective-C," 
    Copyright © 2004 by Sams Publishing. Used with permission</SPAN></LI>
    <LI>main.m<PRE class=code style="BORDER-RIGHT: gray 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: gray 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: gray 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: gray 1px solid; BACKGROUND-COLOR: rgb(238,238,238)">#import &lt;stdio.h&gt;
#import "Fraction.h"

int main( int argc, const char *argv[] ) {
    // create a new instance
    Fraction *frac = [[Fraction alloc] init];

    // set the values
    [frac setNumerator: 1];
    [frac setDenominator: 3];

    // print it
    printf( "The fraction is: " );
    [frac print];
    printf( "\n" );

    // free memory
    [frac release];

    return 0;
}</PRE></LI>
    <LI>output<PRE class=output style="BORDER-RIGHT: gray 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: gray 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: gray 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: gray 1px solid; BACKGROUND-COLOR: rgb(238,231,192)">The fraction is: 1/3</PRE></LI>
    <LI>Fraction *frac = [[Fraction alloc] init];</LI>
    <UL>
      <LI>There are several important things in this one line.</LI>
      <LI>The way methods in Objective-C are called is [object method], which is 
      similar to object-&gt;method() in C++</LI>
      <LI>Objective-C doesn't have value types, so there is nothing similar to 
      C++'s: Fraction frac; frac.print();. You always deal with objects as 
      pointers in Objective-C.</LI>
      <LI>What this line is really doing is two things: [Fraction alloc] is 
      calling the alloc method on the Fraction class. This is similar to 
      mallocing memory, because that is all that is done in this operation.</LI>
      <LI>[object init] is the constructor call, which initializes any variables 
      in the object. This method is called on the instance returned from 
      [Fraction alloc]. This operation is so common it's usually just done in 
      one line as Object *var = [[Object alloc] init];</LI></UL>
    <LI>[frac setNumerator: 1] is quite simple. It's calling the setNumerator 
    method on frac, and passing it the parameter 1.</LI>
    <LI>Like every c variant, there's a construct for freeing memory. This is 
    done via release, which is inherited from NSObject. This method will be 
    explainted in greater detail later.</LI></UL></UL></body>
</html>
